//
//  YWMyDemandCell.m
//  百布业务
//
//  Created by 陈炳桦 on 15/10/30.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//

#import "YWMyDemandCell.h"
#import "YWMyDemand.h"
#import <UIImageView+WebCache.h>


@interface YWMyDemandCell()
// 状态图片
@property (weak, nonatomic) IBOutlet UIImageView *statusView;
// 主图片
@property (weak, nonatomic) IBOutlet UIImageView *photoView;
//需求编号
@property (weak, nonatomic) IBOutlet UILabel *demandLabel;
// 描述
@property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
// 红色的描述
@property (weak, nonatomic) IBOutlet UILabel *redDescriptionLabel;
// 商户接单数
@property (weak, nonatomic) IBOutlet UILabel *relpyLabel;
// 时间
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation YWMyDemandCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setMydemand:(YWMyDemand *)mydemand{
    _mydemand = mydemand;
    
    // 设置图片
    if (_mydemand.urls.count > 0) {
        NSURL *url = [NSURL URLWithString:_mydemand.urls[0]];
        [self.photoView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_no_img"]];
    }else{
        self.photoView.image = [UIImage imageNamed:@"default_no_img"];
    }
    
    // 设置状态图片
    if ([_mydemand.status isEqualToString:@"6"]) { // 已找到
        self.statusView.image = [UIImage imageNamed:@"demand_finded_icon"];
    }else if ([_mydemand.status isEqualToString:@"0"]) { // 审核中
        self.statusView.image = [UIImage imageNamed:@"demand_pending"];
    }else if ([_mydemand.status isEqualToString:@"2"]) { // 不通过
        self.statusView.image = [UIImage imageNamed:@"demand_nopass_icon"];
    }else{  // 审核通过 寻找中
        self.statusView.image = [UIImage imageNamed:@"demand_finding_icon"];
    }
    
    // 设置需求编号
    self.demandLabel.text = [NSString stringWithFormat:@"需求编号：%@",_mydemand.ID];
    
    // 设置黑色的描述
    self.descriptionLabel.text = _mydemand.sampleDescribe;
    
    // 设置红色的描述
    self.redDescriptionLabel.text = _mydemand.auditorDesc;
    
    // 设置客户接单数
    if ([_mydemand.replyCount isEqualToString:@"0"]) {
        self.relpyLabel.text = @"暂无商户接单";
    }else{
        self.relpyLabel.text = [NSString stringWithFormat:@"%@个商户接单",_mydemand.replyCount];
    }
    
    // 设置时间
    self.timeLabel.text = [self getTime:_mydemand.createTimeFormat];
}

// 计算时间
- (NSString *)getTime:(NSString *)timeString{
    NSString *day = nil;
    NSString *time = nil;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [formatter dateFromString:timeString];
    
    if ([date isToday]) {   // 今天
        day = @"今天";
    }else if([date isYesterday]){  // 昨天
        day = @"昨天";
    }else{      // 其他 年月日
        formatter.dateFormat = @"MM-dd";
        day = [formatter stringFromDate:date];
    }
    
    formatter.dateFormat = @"HH:mm";
    time = [formatter stringFromDate:date];
    
    return [NSString stringWithFormat:@"%@ %@",day,time];
}

// 重写setFrame实现间距
- (void)setFrame:(CGRect)frame
{
    int space = 5;
    frame.size.height = 120;
    frame.origin.y += space;
    
    [super setFrame:frame];
}


@end
